package com.hamster.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hamster.crm.pojo.BaseEntity;
import com.hamster.crm.service.BaseService;
import java.util.List;
import java.util.Objects;

/**
 * Created by peichunting on 19/12/19.
 */
public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M,T> implements BaseService<T>{

        public static final String DELETE_STATUS = "1";
        public static final String NOMAL_STATUS = "0";

        @Override
        public boolean deletes(List<String> ids){
           List<T> records = this.listByIds(ids);
           if(Objects.isNull(records) || records.size() == 0){
               return true;
           }
           records.forEach(record -> record.setDeleteStatus(DELETE_STATUS));
           return updateBatchById(records);
        }

        @Override
        public T get(String id){
            QueryWrapper<T> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(T.ID,id);
            queryWrapper.eq(T.DELETE_STATUS,NOMAL_STATUS);
            return this.getOne(queryWrapper);
        }

        @Override
        public List<T> listAll(){
            QueryWrapper<T> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(T.DELETE_STATUS,NOMAL_STATUS);
            return list(queryWrapper);
        }



}
